package com.mdroid.util;

import java.io.Closeable;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.Intent.ShortcutIconResource;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;

/**
 * Collection of utility functions used in this package.
 */
public class Util {
    public static final int DIRECTION_LEFT = 0;
    public static final int DIRECTION_RIGHT = 1;
    public static final int DIRECTION_UP = 2;
    public static final int DIRECTION_DOWN = 3;
    
    private static boolean sDensityResolved = false;
    private static float sDensity = -1.0f;

    private Util() {
    }

    /**
    * @param array
    * @param s
    * @return s在array中的位置
    */
    public static <T> int indexOf(T[] array, T s) {
        for (int i = 0; i < array.length; i++) {
            if (array[i].equals(s)) {
                return i;
            }
        }
        return -1;
    }

    public static void closeSilently(Closeable c) {
        if (c == null)
            return;
        try {
            c.close();
        } catch (Throwable t) {
            // do nothing
        }
    }

    public static void Assert(boolean cond) {
        if (!cond) {
            throw new AssertionError();
        }
    }

    public static Animation slideOut(View view, int to) {
        view.setVisibility(View.GONE);
        Animation anim;
        switch (to) {
        case DIRECTION_LEFT:
            anim = new TranslateAnimation(0, -view.getWidth(), 0, 0);
            break;
        case DIRECTION_RIGHT:
            anim = new TranslateAnimation(0, view.getWidth(), 0, 0);
            break;
        case DIRECTION_UP:
            anim = new TranslateAnimation(0, 0, 0, -view.getHeight());
            break;
        case DIRECTION_DOWN:
            anim = new TranslateAnimation(0, 0, 0, view.getHeight());
            break;
        default:
            throw new IllegalArgumentException(Integer.toString(to));
        }
        view.startAnimation(anim);
        return anim;
    }

    public static Animation slideIn(View view, int from) {
        view.setVisibility(View.VISIBLE);
        Animation anim;
        switch (from) {
        case DIRECTION_LEFT:
            anim = new TranslateAnimation(-view.getWidth(), 0, 0, 0);
            break;
        case DIRECTION_RIGHT:
            anim = new TranslateAnimation(view.getWidth(), 0, 0, 0);
            break;
        case DIRECTION_UP:
            anim = new TranslateAnimation(0, 0, -view.getHeight(), 0);
            break;
        case DIRECTION_DOWN:
            anim = new TranslateAnimation(0, 0, view.getHeight(), 0);
            break;
        default:
            throw new IllegalArgumentException(Integer.toString(from));
        }
        view.startAnimation(anim);
        return anim;
    }

    public static <T> T checkNotNull(T object) {
        if (object == null)
            throw new NullPointerException();
        return object;
    }

    public static boolean equals(Object a, Object b) {
        return (a == b) || (a == null ? false : a.equals(b));
    }

    public static boolean isPowerOf2(int n) {
        return (n & -n) == n;
    }

    public static int nextPowerOf2(int n) {
        n -= 1;
        n |= n >>> 16;
        n |= n >>> 8;
        n |= n >>> 4;
        n |= n >>> 2;
        n |= n >>> 1;
        return n + 1;
    }

    public static float distance(float x, float y, float sx, float sy) {
        float dx = x - sx;
        float dy = y - sy;
        return (float) Math.sqrt(dx * dx + dy * dy);
    }

    public static int clamp(int x, int min, int max) {
        if (x > max)
            return max;
        if (x < min)
            return min;
        return x;
    }

    public static int getDisplayRotation(Activity activity) {
        return 0;
    }
    
    /**
     * The logical density of the display.
     */
    public static float getDensity(Context context) {
        float density = sDensity;
        if (!sDensityResolved) {
            density = context.getResources().getDisplayMetrics().density;
            density = density / 1.5f;
            sDensity = density;
            sDensityResolved = true;
        }
        return density;
    }

    public static int getWidthPx(Context mContext) {
        if (mContext instanceof Activity) {
            DisplayMetrics dm = new DisplayMetrics();
            ((Activity) mContext).getWindowManager().getDefaultDisplay()
                    .getMetrics(dm);

            return dm.widthPixels;
        }
        return 0;
    }
    
    /**
     * 需要加权限: com.android.launcher.permission.INSTALL_SHORTCUT
     * @author William.cheng
     * @param nameId 快捷方式名字的Id
     * @param iconId 快捷方式图标的Id
     */
    public static void addShortcut(Activity activity, int nameId, int iconId) {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setClassName(activity, activity.getClass().getName());
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        Intent shortcutIntent = new Intent();
        shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
        shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, activity.getString(nameId));
        ShortcutIconResource shortcuticonresource = ShortcutIconResource.fromContext(activity, iconId);
        shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcuticonresource);
        shortcutIntent.putExtra("duplicate", false);
        
//      shortcutIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
        shortcutIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        activity.sendBroadcast(shortcutIntent);
    }

}
